﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AMS.DataAccess.Common
{
    /// <summary>
    /// 类型转换类
    /// </summary>
    public class AdoConverter
    {
        /// <summary>
        /// 表示空的 GUID
        /// </summary>
        public static readonly Guid NullGuid = new Guid();

        /// <summary>
        /// 将对象转换成字符串，如果是 null 或 DBNull.Value 都返回 null
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToString(object value)
        {
            if (value == null) return null;
            if (value == DBNull.Value) return null;
            return Convert.ToString(value);
        }

        /// <summary>
        /// 转换成 int，如果是 null 或 DBNull.Value 则返回 0
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int ToInt32(object value)
        {
            if (null == value || DBNull.Value == value)
            {
                return 0;
            }

            return Convert.ToInt32(value);
        }

        /// <summary>
        /// 转换成 boolean，如果是 null 或 DBNull.Value 则返回 false
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool ToBoolean(object value)
        {
            if (null == value || DBNull.Value == value)
            {
                return false;
            }

            return Convert.ToBoolean(value);
        }

        /// <summary>
        /// 转换成 long，如果是 null 或 DBNull.Value 则返回 0
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static long ToInt64(object value)
        {
            if (null == value || DBNull.Value == value)
            {
                return 0;
            }

            return Convert.ToInt64(value);
        }

        /// <summary>
        /// 转换成 Guid，如果是 null 或 DBNull.Value 则返回 Guid.Empty
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Guid ToGuid(object value)
        {
            if (null == value || DBNull.Value == value)
            {
                return Guid.Empty;
            }

            return (Guid)value;
        }

        /// <summary>
        /// 转换成 short，如果是 null 或 DBNull.Value 则返回 0
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static short ToShort(object value)
        {
            if (null == value || DBNull.Value == value)
            {
                return 0;
            }

            return Convert.ToInt16(value);
        }

        /// <summary>
        /// 转换成 decimal，如果是 null 或 DBNull.Value 则返回 0
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static decimal ToDecimal(object value)
        {
            if (null == value || DBNull.Value == value)
            {
                return 0m;
            }

            return Convert.ToDecimal(value);
        }

        /// <summary>
        /// 转换成 DateTime，如果是 null 或 DBNull.Value 则返回 new DateTime()
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(object value)
        {
            if (null == value || DBNull.Value == value)
            {
                return new DateTime();
            }

            return Convert.ToDateTime(value);
        }

        /// <summary>
        /// 转换成枚举类型，如果是 null 或 DBNull.Value 则返回枚举的第一个值
        /// </summary>
        /// <param name="value"></param>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static object ToEnum(object value, Type enumType)
        {
            if (null == value || DBNull.Value == value)
            {
                return Enum.GetValues(enumType).GetValue(0);
            }
            else
            {
                if (value is string)
                {
                    return Enum.Parse(enumType, (string)value);
                }

                return Enum.ToObject(enumType, value);
            }
        }
    }
}
